apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service"], function(V, $api, $, app, service) {
        var vm = {
            el: '.container',
            data: {
                star: '',
                shopName: '',
                logo: '../../../image/shop/list-01.png',
                shopId: '',
                name: '',
                ShopRedTip:{}
            },
            created: function() {
                var that = this;
                this.init();
                this.star = $api.getStorage('user').data.star;
                api.addEventListener({
                    name: 'saveShopInfo'
                }, function(ret, err) {
                    if (ret) {
                        that.init();
                    }
                });
                this.name = api.pageParam.name;
            },
            mounted: function() {
                var that = this;
                var shop_data = $api.getStorage("shop_data");
                if (shop_data.is_seller == 0) {
                    shop_data.is_seller = 1;
                    $api.setStorage("shop_data", shop_data);
                    //console.log("myShop："+JSON.stringify($api.getStorage("shop_data")));
                    api.sendEvent({
                        name: 'closeOpenShop'
                    });
                }

                function btnKeyBack() {
                    api.sendEvent({
                        name: 'closeOpenShop'
                    });
                    $api.setTimeout(function() {
                        api.closeWin();
                    }, 300)
                }

                //--点击上面按钮返回
                $api.addEvt($api.dom('#btn-back-new'), 'click', function() {
                    btnKeyBack();
                });

                //--返回键
                api.addEventListener({
                    name: 'keyback'
                }, function(ret, err) {
                    btnKeyBack();
                });
                //--右滑
                api.addEventListener({
                    name: 'swiperight'
                }, function(ret, err) {
                    btnKeyBack();
                });
                //监听红点
                api.addEventListener({
                    name: 'shopRed'
                }, function(ret, err){
                  that.ShopRedTip = ret.value.ShopRedTip
                });

            },
            methods: {
                init: function() {
                    var that = this;
                    var appId = app.appid;
                    var unionid = $api.getStorage("shop_unionid");
                    var userid = $api.getStorage("user").data.out_user_id;
                    app.ajax(
                        service['shopDetail'], {
                            data: {
                                values: {
                                    appid: appId,
                                    userid: userid
                                }
                            }
                        },
                        function(res) {
                            if (res.code == 0) {
                                if (res.data.length == 0) {
                                    app.openWin('createdShop');
                                } else {
                                    var shopCode = res.data.shop_code; // 商店id
                                    var shopName = res.data.shop_name;
                                    var shopLogo = res.data.shop_logo;
                                    that.shopName = shopName;
                                    that.logo = shopLogo;
                                    that.shopId = shopCode;
                                }
                            }
                        }
                    );
                    this.getShopRedTip()
                },
                // 获取提示标识
                getShopRedTip:function(){
                  var that = this
                    app.ajax(
                        service.getShopRedTip, {
                            data: {
                                values: {
                                    shop_code:$api.getStorage('user').data.shop_code,
                                    mask:"none"
                                }
                            }
                        },
                        function(res) {
                            if (res.code==0) {
                              that.ShopRedTip = res.data
                            }
                        })
                },
                toShopCard: function() {
                    var that = this;
                    app.openWin('shopCard', {
                        shopId: that.shopId
                    })
                },
                //--店铺装修
                myShopAmend: function(et) {
                    api.openWin({
                        name: 'single_shop',
                        url: '../shop_index/single_shop.html',
                        pageParam: {
                            out_user_id: $api.getStorage('user').data.out_user_id
                        },
                        reload: true
                    })
                },
                //--添加商品
                myShopAddGoods: function(et) {
                    var that = this;
                    app.openWin('shopAdd', {
                        shopId: that.shopId
                    })
                },
                //--添加会员商品
                myShopAddMemberGoods: function(et) {
                    var that = this;
                    app.ajax(service['getGoodsList'], {
                        data: {
                            values: {
                                shop_code: $api.getStorage('user').data.shop_code,
                                is_member_product: 1,
                            }
                        }
                    }, function(res) {
                        if (res.data.length > 0) {
                            app.openWin('shopEditMember', {
                                shopId: that.shopId
                            })
                        } else {
                            app.openWin('shopAddMember', {
                                shopId: that.shopId
                            })
                        }
                    });
                },
                //--商品管理
                myShopGoodsManage: function(et) {
                    var that = this;
                    app.openWin('goodsManage', {
                        shopId: that.shopId
                    })
                },
                applyToExposure: function() {
                    var that = this;
                    app.ajax(service['getApplyRecommend'], {
                        data: {
                            values: {
                                shop_code: this.shopId
                            }
                        }
                    }, function(res) {
                        if (JSON.stringify(res.data) != '[]') {
                            app.openWin('applyToExposureList', {
                                shopId: that.shopId,
                                goodsObj: res.data
                            })
                        } else {
                            app.openWin('applyToExposure', {
                                shopId: that.shopId
                            })
                        }
                    })
                },
                //邀请开店
                getQRCode: function() {
                    var that = this;
                    var photoBrowser = api.require('photoBrowser');
                    app.ajax(service['createPict'], {
                        data: {
                            values: {
                                mask:"none"
                            }
                        }
                    }, function(res) {
                      if(res.code==0){
                        photoBrowser.open({
                            images: [res.data],
                            bgColor: '#000'
                        }, function(ret, err) {
                            if (ret.eventType == 'click') {
                                photoBrowser.close();
                            }
                            if (ret.eventType == 'longPress') {
                                that.upFile(res.data, function() {
                                    app.toast('已下载至手机相册')
                                })
                            }
                        });
                      }
                    })
                },
                // 下载文件
                upFile: function(url, cb) {
                    var num = Math.random() + '';
                    var list = num.split('.');
                    api.download({
                        url: url,
                        cache: true,
                        savePath: 'fs://DCIM/Camera/' + list[1] + '.png',
                        allowResume: true
                    }, function(ret, err) {
                        if (ret.state == 1) {
                            api.saveMediaToAlbum({
                                path: 'fs://DCIM/Camera/' + list[1] + '.png'
                            }, function(ret, err) {
                                if (ret && ret.status) {
                                    cb('fs://DCIM/Camera/' + list[1] + '.png')
                                }
                            })
                        }
                        if (ret.state == 2) {
                            api.toast({
                                msg: ret.msg,
                                duration: 2000,
                                location: 'bottom'
                            })
                        }
                    });
                },
                //--订单记录
                myShopOrderRecord: function(et) {
                    app.openWin('shopOrder', {
                        shopId: this.shopId
                    }, {
                        reload: true
                    })
                },
                //--店铺活动
                myShopActivity: function(et) {
                    app.openWin('shopActivity', {
                        shopId: this.shopId
                    })
                },
                //--评价管理
                myShopEvaluateManage: function() {
                    app.openWin('commentManage', {
                        shopId: this.shopId,
                        slidBackEnabled:false
                    })
                },

                // 推荐分成
                toDivide: function() {
                    // alert('敬请期待...');
                    app.openWin('divideInto');
                },

                //我的收入
                openMyIncome: function() {
                    app.openWin('shopIncome', {
                        shopId: this.shopId
                    }, {
                        reload: true
                    });
                }


            }
        };
        var even = function() {}
        var newVue = new Vue(vm);
        even();

    })
}
